package com.yaohe.consumer.network;

import java.util.HashMap;

import org.apache.mina.core.session.IoSession;

import com.yaohe.consumer.network.msghandler.MessageHandler;
import com.yaohe.protocols.ProtocolProtos.Protocol;

public class ResponseHandler {
	private static ResponseHandler theInstance = new ResponseHandler ();
	private HashMap <Protocol.FunctionType, MessageHandler> subHandlers;
	
	protected ResponseHandler (){
		this.subHandlers = new HashMap <Protocol.FunctionType, MessageHandler>  ();
	}
	public static ResponseHandler getInstance (){
		return theInstance;
	}
	
	public void handle(Protocol message, IoSession session) {
		Protocol.FunctionType fType = message.getFunctionType();
		MessageHandler subHandler = subHandlers.get(fType);
		
		if (subHandler == null){
			return;
		}		
		else {
			subHandler.handle(message, session);
		}
	}
	
	public boolean registerSubHandler (Protocol.FunctionType fType, MessageHandler handler){
		if (subHandlers.get(fType) == null){
			subHandlers.put(fType, handler);
			return true;
		}else {
			
			return false;
		}
	}
}
